import vue from "@vitejs/plugin-vue";
import vueJsx from "@vitejs/plugin-vue-jsx";
import svgLoader from "vite-svg-loader";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import Icons from "unplugin-icons/vite";
import IconsResolver from "unplugin-icons/resolver";
import { ElementPlusResolver, VueUseComponentsResolver } from "unplugin-vue-components/resolvers";
import WindiCSS from "vite-plugin-windicss";
import Markdown from "vite-plugin-vue-markdown";
import Prism from "markdown-it-prism";
import Unfonts from "unplugin-fonts/vite";
import VueI18n from "@intlify/vite-plugin-vue-i18n";
// @ts-ignore
import LinkAttributes from "markdown-it-link-attributes";
import { ConfigEnv } from "vite";
import { resolve } from "path";

const defaultClasses = "prose prose-sm m-auto text-left";

export default (env: ConfigEnv) => {
  return [
    vue({
      include: [/\.vue$/, /\.md$/],
    }),
    vueJsx(),
    svgLoader(),
    AutoImport({
      dts: "./src/auto-imports.d.ts",
      // @ts-ignore
      imports: ["vue", "pinia", "vue-router", "vue-i18n", "@vueuse/core"],
      // Generate corresponding .eslintrc-auto-import.json file.
      // eslint globals Docs - https://eslint.org/docs/user-guide/configuring/language-options#specifying-globals
      eslintrc: {
        enabled: false, // Default `false`
        filepath: "./.eslintrc-auto-import.json", // Default `./.eslintrc-auto-import.json`
        globalsPropValue: true, // Default `true`, (true | false | 'readonly' | 'readable' | 'writable' | 'writeable')
      },
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      dts: "./src/components.d.ts",
      extensions: ["vue", "md"],
      include: [/\.vue$/, /\.vue\?vue/, /\.md$/],
      // imports 指定组件所在位置，默认为 src/components; 有需要也可以加上 view 目录
      dirs: ["src/components/"],
      resolvers: [ElementPlusResolver(), IconsResolver(), VueUseComponentsResolver()],
    }),
    Icons({
      compiler: "vue3",
      autoInstall: true,
    }),
    Unfonts({
      google: {
        families: ["Open Sans", "Montserrat", "Fira Sans"],
      },
      custom: {
        families: [
          {
            name: "Game Font",
            src: "./src/assets/fonts/*.woff2",
          },
        ],
      },
    }),
    VueI18n({
      include: [resolve(__dirname, "../locales/**")],
    }),
    WindiCSS({
      safelist: defaultClasses,
    }),
    Markdown({
      wrapperClasses: defaultClasses,
      headEnabled: false,
      markdownItSetup(md) {
        // https://prismjs.com/
        md.use(Prism);
        // 为 md 中的所有链接设置为 新页面跳转
        md.use(LinkAttributes, {
          matcher: (link: string) => /^https?:\/\//.test(link),
          attrs: {
            target: "_blank",
            rel: "noopener",
          },
        });
      },
    }),
  ];
};
